using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    [Serializable]
    public class FileMoveWorker:IWorker
    {
        private Thread _WorkerThread;
        private FileMoveInstruction _Instruction;
        private FileMoveOutput _Output;
        private Guid _TicketID;
        private RunContext _Context;
        private string _WorkflowName;
        private string _ComponentName;

        /// <summary>
        /// 
        /// </summary>
        public string ComponentName
        {
            get { return _ComponentName; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string WorkflowName
        {
            get { return _WorkflowName; }
        }

        public Thread WorkerThread
        {
            get { return _WorkerThread; }
        }

        public InstructionBase Instruction
        {
            get { return _Instruction; }
        }

        public ResultBase Output
        {
            get { return this._Output; }
        }

        public Type ComponentType
        {
            get { return typeof(FileMoveComponent); }
        }

        public RunContext Context
        {
            get { return _Context; }
            set { _Context = value; }
        }

        public void SetInstruction(Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._TicketID = ticketGuid;
            this._Instruction = (FileMoveInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        public void SetInstruction(RunContext context, Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._Context = context;
            this._TicketID = ticketGuid;
            this._Instruction = (FileMoveInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        public void DoWork()
        {
            if(this.WorkerStartedEvent !=null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);
            }
            this._WorkerThread = Thread.CurrentThread;
            this.MoveFiles();
        }

        public void DoWorkAsync()
        {
            if (this.WorkerStartedEvent != null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);
            }
            this._WorkerThread = new Thread(new ThreadStart(this.MoveFiles));
            this._WorkerThread.Start();
        }

        public void CancelWork()
        {
            if(this._WorkerThread !=null && this._WorkerThread.IsAlive)
            {
                this._WorkerThread.Abort();
                if(this.WorkerCancelledEvent !=null)
                {
                    this.WorkerCancelledEvent(this._TicketID, this.GetType(), this._Instruction);
                }
            }
        }

        public event WorkerStarted WorkerStartedEvent;
        public event WorkerFinished WorkerFinishedEvent;
        public event WorkerHasError WorkerErredEvent;
        public event WorkerCancelled WorkerCancelledEvent;
        public event WorkerStatusChanged WorkerStatusChangedEvent;

        #region work
        private void MoveFiles()
        {
            try
            {
                List<string> filesMoved=new List<string>();
                int count = 0;
                foreach(string srcFilePath in this._Instruction.SrcFiles)
                {
                    string tgtPath = Path.Combine(this._Instruction.TgtFolder, Path.GetFileName(srcFilePath));
                    if(File.Exists(tgtPath))
                    {
                        File.Delete(tgtPath);
                    }
                    File.Move(srcFilePath,tgtPath);
                    filesMoved.Add(tgtPath);
                    count++;

                    if(this.WorkerStatusChangedEvent !=null)
                    {
                        string msg =
                            string.Format("Moved file {0}. #{1} of {2}...", Path.GetFileName(srcFilePath), count,
                                          this._Instruction.SrcFiles.Count);
                        int percent = (count*100)/this._Instruction.SrcFiles.Count;
                        this.WorkerStatusChangedEvent(this._TicketID, this.GetType(), this._Instruction,
                            this.WorkflowName, this.ComponentName, msg, percent);
                    }
                }
                this._Output =
                    new FileMoveOutput(this._Instruction.OwnerComponentID, this._TicketID, filesMoved);
                    
                if(this.WorkerFinishedEvent!=null)
                {
                    this.WorkerFinishedEvent(this._TicketID, this.GetType(), this._Instruction, this._Output);
                }
            }
            catch(Exception ex)
            {
                if(this.WorkerErredEvent !=null)
                {
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this._Instruction, ex.ToString(),true);
                }
            }
        }
        #endregion
    }
}
